﻿Imports FastReport.Report
Imports DevExpress.XtraGrid.Views.Grid
Public Class frmNewDrawForm
    Public Primary_Key As Integer = -1
    Public Primary_Detail_Key = -1
    Public Curr_RowID As Integer = -1
    Public DocNO As Integer = 0
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
    Public Report_Name As String
#Region "Function"
    Function Get_NewDrawDocNO() As String
        Try
            Dim LasID As Integer = 0
            Strsql = "Select ifnull(right(doc_no,2),0) as lastID from document_draw_header where month(doc_date) = " & Now.Month & " and year(doc_date) = " & Now.Year & " order by doc_id desc limit 1"
            LasID = _mysql.MySQLExecuteScalar(Strsql)
            Get_NewDrawDocNO = (Now.Year + 543).ToString & "-" & Now.Month.ToString("0#") & "/"
            If Len((LasID + 1).ToString) = 1 Then
                Get_NewDrawDocNO = Get_NewDrawDocNO & "0" & (LasID + 1).ToString

            End If
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Get_NewDrawDocNO"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Function
    Sub Load_Doctype()
        Try
            Dim DT_DocType As New DataTable
            Strsql = "Select doc_type_ID,doc_type_name from doc_type order by doc_type_name "
            DT_DocType = _mysql.GetMYSQLDataTable(Strsql, "doc_type")
            With CboDocType

                .DataSource = DT_DocType
                .DisplayMember = "doc_type_name"
                .ValueMember = "doc_type_ID"
                .Text = DT_DocType.Rows(0).Item("doc_type_name")
            End With
          
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Doctype"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Department()
        Try
            Dim DT_Department As New DataTable
            Strsql = "Select dep_id,dep_name from departments order by dep_name "
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "departments")
            With CboDepartments

                .Properties.DataSource = DT_Department
                .Properties.DisplayMember = "dep_name"
                .Properties.ValueMember = "dep_id"
                .Text = DT_Department.Rows(0).Item("dep_name")
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Department"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Doc_Status()
        Try
            Dim DT_Department As New DataTable
            Strsql = "Select status_id,status_name from doc_status  order by status_name "
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "doc_status")
            With CboStatus

                .Properties.DataSource = DT_Department
                .Properties.DisplayMember = "status_name"
                .Properties.ValueMember = "status_id"
                .Text = DT_Department.Rows(0).Item("status_name")
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Doc_Status"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Data()
        Try
            Strsql = "select doc_detail_id,doc_head_id,doc_rowid,productname,serialno,band,model,priceperunit,vat,priceincludevat,drawqty,receiveqty,totalpriceprice,staff,remarks,purchase_date,mdf_date,warantry from document_draw_detail where doc_head_id = " & Primary_Key
            Dim DT_document_draw_detail As New DataTable
            DT_document_draw_detail = _mysql.GetMYSQLDataTable(Strsql, "document_draw_detail")

            GridControl1.DataSource = DT_document_draw_detail


        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
#End Region
    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        If Primary_Key = -1 Then
            If MsgBox("การจะเพิ่มสินค้าได้จำเป็นต้องบันทึกก่อนครับ คุณต้องการที่จะบันทึก่อนหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub

            Strsql = "Insert into document_draw_header (doc_no,doc_date,doc_department,doc_remarks,doc_recevie_date,doc_status,doc_type,doc_lastupdate,doc_place) "
            Strsql = Strsql & " values('" & Me.TxtDocNo.Text & "','" & Pn_Framework.MysqlDateTimeFormat(Me.DtpDocDate.DateTime, False) & "','" & Me.CboDepartments.EditValue & "','"
            Strsql = Strsql & Me.TxtRemark.Text & "','" & Pn_Framework.MysqlDateTimeFormat(Me.DTPReceiveDate.DateTime, False) & "'," & Me.CboStatus.EditValue & ",2,'" & Pn_Framework.MysqlDateTimeFormat(Now, False) & "','" & Me.TxtPlace.Text & "')"
            If _mysql.MySQLExecute(Strsql) > 0 Then
                MsgBox("บันทึกข้อมูลเอกสารเรียบร้อย")
                Primary_Key = Pn_Framework.Get_LastNumericInsertID()
            Else
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnAdd_Click"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With
            End If


        End If

        If GridView1.RowCount > 1 Then Curr_RowID = GridView1.RowCount + 1
        frmNewDrawItem.Primary_Key = Primary_Key
        frmNewDrawItem.New_Rows = Curr_RowID
        If frmNewDrawItem.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub
        Try
            Load_Data()
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnAdd_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub frmNewReceiveForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TxtDocNo.Text = Get_NewDrawDocNO()
        Me.DTPReceiveDate.DateTime = Now
        Me.DtpDocDate.DateTime = Now
        Load_Department()
        Load_Doc_Status()
        Load_Doctype()
    End Sub

    Private Sub PanelControl1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PanelControl1.Paint

    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        If Primary_Key = -1 Then
            If MsgBox("การจะเพิ่มสินค้าได้จำเป็นต้องบันทึกก่อนครับ คุณต้องการที่จะบันทึก่อนหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub

            If Me.DtpDocDate.DateTime = Now Then
                MsgBox("คำเตือนวันที่ที่ระบุตรงกับวันที่ปัจจุบัน", MsgBoxStyle.Exclamation)
            End If

            Strsql = "Insert into document_draw_header (doc_no,doc_date,doc_department,doc_remarks,doc_recevie_date,doc_status,doc_type,doc_lastupdate) "
            Strsql = Strsql & " values('" & Me.TxtDocNo.Text & "','" & Pn_Framework.MysqlDateTimeFormat(Me.DtpDocDate.DateTime, False) & "','" & Me.CboDepartments.EditValue & "','"
            Strsql = Strsql & Me.TxtRemark.Text & "','" & Pn_Framework.MysqlDateTimeFormat(Me.DTPReceiveDate.DateTime, False) & "'," & Me.CboStatus.EditValue & "," & Me.CboDocType.SelectedValue & ",'" & Pn_Framework.MysqlDateTimeFormat(Now, False) & "')"
            If _mysql.MySQLExecute(Strsql) > 0 Then
                Primary_Key = Pn_Framework.Get_LastNumericInsertID()
                MsgBox("บันทึกข้อมูลเอกสารเรียบร้อย", MsgBoxStyle.Information, "ผลการบันทึกข้อมูล")
                frmDrawList.Load_Data()
            Else
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnSave_Click"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With
            End If
        Else


            Strsql = "Update document_draw_header set doc_no='" & Me.TxtDocNo.Text & "',doc_date='" & Pn_Framework.MysqlDateTimeFormat(Me.DtpDocDate.DateTime, False) & "',doc_department=" & Me.CboDepartments.EditValue & ",doc_remarks='" & Me.TxtRemark.Text & "'"
            Strsql = Strsql & ",doc_recevie_date='" & Pn_Framework.MysqlDateTimeFormat(Me.DTPReceiveDate.DateTime, False) & "',doc_status = " & Me.CboStatus.EditValue & ",doc_lastupdate ='" & Pn_Framework.MysqlDateTimeFormat(Now, False) & "' where doc_id = " & Primary_Key
            If _mysql.MySQLExecute(Strsql) >= 0 Then
                MsgBox("บันทึกข้อมูลเอกสารเรียบร้อย", MsgBoxStyle.Information, "ผลการบันทึกข้อมูล")
                frmDrawList.Load_Data()
            Else
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnSave_Click"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With
            End If
        End If

    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub
    Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
        Try
            Strsql = "SELECT report_id,report_name,report_formname,report_filename,report_group from report_template where report_id = 3"
            Dim DT_Reportlist As New DataTable
            DT_Reportlist = _mysql.GetMYSQLDataTable(Strsql, "report_template")
            Report_Name = DT_Reportlist.Rows(0).Item("report_filename").ToString
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Try

            With frmPrintDrawForm
                .Primary_Key = Primary_Key
                .Report_Name = Report_Name
                .MdiParent = frmmain
                .WindowState = FormWindowState.Maximized
                .Show()
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnPrint_Click"
                .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        If MsgBox("คุณต้องการลบข้อมูล 'สินค้า' รหัส " & Primary_Detail_Key & " ออกจากระบบหรือไม่?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub

        Try
            Strsql = "delete from document_draw_detail where doc_detail_id = " & Primary_Detail_Key
            If _mysql.MySQLExecute(Strsql) < 0 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnDelete_Click : delete detail"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With
            Else
                MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการลบ")
                Load_Data()
            End If
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnPrint_Click"
                .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Dim Gv As GridView = sender
        Primary_Detail_Key = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("doc_detail_id"))
    End Sub

    Private Sub GridControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridControl1.Click

    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        If Primary_Detail_Key = -1 Then Exit Sub
        frmEditDrawItem.Primary_Key = Primary_Detail_Key
        If frmEditDrawItem.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub

        Load_Data()

    End Sub

    Private Sub BtnPrintPR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrintPR.Click
        Try
            Strsql = "SELECT report_id,report_name,report_formname,report_filename,report_group from report_template where report_id = 1"
            Dim DT_Reportlist As New DataTable
            DT_Reportlist = _mysql.GetMYSQLDataTable(Strsql, "report_template")
            Report_Name = DT_Reportlist.Rows(0).Item("report_filename").ToString
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Try

            With frmPrintDrawForm
                .Primary_Key = Primary_Key
                .Report_Name = Report_Name
                .MdiParent = frmmain
                .WindowState = FormWindowState.Maximized
                .Show()
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnPrintPR_Click"
                .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub BtnClone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClone.Click
        If Primary_Detail_Key = -1 Then

        End If
        If MsgBox("คุณต้องคัดลอกข้อมูลสินค้า Record ที่ " & Primary_Detail_Key & " เป็น Record ใหม่ใช่หรือไม่?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการคัดลอก") = MsgBoxResult.No Then Exit Sub

        Try
            Strsql = "Insert into document_draw_detail (doc_head_id,doc_rowid,productname,serialno,band,model,totalpriceprice,drawqty,receiveqty,staff,remarks,purchase_date,mdf_date,warantry,priceincludevat,vat,priceperunit,nonprice,nonmfd,nonbuydate,nonvat,imagepart) "
            Strsql = Strsql & " select  doc_head_id,doc_rowid,productname,serialno,band,model,totalpriceprice,drawqty,receiveqty,staff,remarks,purchase_date,mdf_date,warantry,priceincludevat,vat,priceperunit,nonprice,nonmfd,nonbuydate,nonvat,imagepart FROM document_draw_detail where doc_detail_id = " & Primary_Detail_Key



            'Strsql = "delete from document_draw_detail where doc_detail_id = " & Primary_Key
            If _mysql.MySQLExecute(Strsql) < 0 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnClone_Click : Copy detail"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                    Exit Sub
                End With


            End If
            Load_Data()
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnClone_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub BtnRepairForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRepairForm.Click
        Try
            Strsql = "SELECT report_id,report_name,report_formname,report_filename,report_group from report_template where report_id = 4"
            Dim DT_Reportlist As New DataTable
            DT_Reportlist = _mysql.GetMYSQLDataTable(Strsql, "report_template")
            Report_Name = DT_Reportlist.Rows(0).Item("report_filename").ToString
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Try

            With frmPrintDrawForm
                .Primary_Key = Primary_Key
                .Report_Name = Report_Name
                .MdiParent = frmmain
                .WindowState = FormWindowState.Maximized
                .Show()
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnRepairForm_Click"
                .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
End Class